package com.glory.brepda.activity.mfg;

import android.os.Handler;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.MaterialAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.WaferEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;


import butterknife.BindView;

public class MaterialIssuingActivity extends BaseActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etTaskCode)
    FreeUI_ClearEditText etTaskCode;
    @BindView(R.id.rvChipList)
    RecyclerView rvChipList;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvCustomerID)
    TextView tvCustomerID;
    @BindView(R.id.tvProcessName)
    TextView tvProcessName;
    @BindView(R.id.etMaterialLotCode)
    FreeUI_ClearEditText etMaterialLotCode;
    @BindView(R.id.etLocAtion)
    FreeUI_ClearEditText etLocAtion;
    private MaterialAdapter chipAdapter;
    String code = "";
    private int number = 0;
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_materialissuing;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        tvBaseSubmit.setText(String.valueOf(number));
        chipAdapter = new MaterialAdapter(null);
        chipAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        chipAdapter.isFirstOnly(true);
        initRecyclerView(rvChipList, this);
        rvChipList.setAdapter(chipAdapter);

    }

    @Override
    protected void initData() {
        initEdit(etTaskCode, s->{

            if (s.length() < 6){
                showToast("任务号格式不正确",false);
                return;
            }

            FreeUI_EntityCallBack<BaseResponseBean<WaferEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<WaferEntity>>
                    (new TypeToken<BaseResponseBean<WaferEntity>>() {
                    }.getType(), getSupportFragmentManager(), MaterialIssuingActivity.this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<WaferEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(MaterialIssuingActivity.this, true, getSupportFragmentManager())) {
                        tvCustomerID.setText(response.body().getResponse().getBody().getCUSTOMERCODE());
                        tvProcessName.setText(response.body().getResponse().getBody().getMAINMATTYPE());
                        etMaterialLotCode.requestFocus();
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<WaferEntity>> response) {
                    super.onError(response);
                    etTaskCode.requestFocus();
                    FreeApi_LogUtils.saveErrorMessages(response.getException(), "MLotOut");
                }
            };
            code = s.substring(0,6);
            etTaskCode.setText(code);
            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE", "QUERYWORKORDER");
            map.addProperty("WOID", code);
            NetUtils.requestPostNet("IF.MLotOut", "Common", MaterialIssuingActivity.this, map, callBack);

        });

        initEdit(etMaterialLotCode, s->{
            etMaterialLotCode.setText("");
            if (code.equals("")){
                showToast("任务号不能为空",false);
                return;
            }
            if (etLocAtion.getText().toString().equals("")){
                showToast("请输入设备位置号",false);
                return;
            }
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    etMaterialLotCode.requestFocus();
                }
            }, 500);
            FreeUI_EntityCallBack<BaseResponseBean<WaferEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<WaferEntity>>
                    (new TypeToken<BaseResponseBean<WaferEntity>>() {
                    }.getType(), getSupportFragmentManager(), MaterialIssuingActivity.this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<WaferEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(MaterialIssuingActivity.this, true, getSupportFragmentManager())) {
                        chipAdapter.addData(0,response.body().getResponse().getBody().getMLOT());
                        int integer = Integer.parseInt(tvBaseSubmit.getText().toString());
                        tvBaseSubmit.setText(String.valueOf(integer + 1));
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<WaferEntity>> response) {
                    super.onError(response);
                    FreeApi_LogUtils.saveErrorMessages(response.getException(), "MLotOut");
                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE", "QUERYMLOTBYMLOTID");
            map.addProperty("WOID", code);
            map.addProperty("MLOTID", s);
            map.addProperty("LOCATION", getText(etLocAtion));
            NetUtils.requestPostNet("IF.MLotOut", "Common", MaterialIssuingActivity.this, map, callBack);

        });

    }
}
